package com.yjt.javatest.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * @author fengye
 * @Date: 2022-08-02-14:04
 * @Description:
 */
public class RandomValue {
    public static Double randomValue(String range, Integer interval, Integer accuracy) {
        Random rand = new Random();

        int i = range.indexOf(",");
        if (i == -1) {
            throw new RuntimeException("range 格式错误");
        }

        Double left = Double.valueOf(range.substring(1, i));
        Double right = Double.valueOf(range.substring(i + 1, range.length() - 1));
        Double r = left;

        //保证rigth大于left
        if (right <= left) {
            throw new RuntimeException("range 格式错误" + range);
        }
        //[1,3)
        Integer runTime;
        //表示取值区间连续
        if (interval == 0) {
            runTime = 0;
            while (true) {
                runTime++;
                if (runTime > 15) {
                    throw new RuntimeException("range设置有误，无法产生合适随机值");
                }
                r = rand.nextDouble() * (right - left) + left;
                r = Double.valueOf(String.format("%." + accuracy + "f", r));
                if (range.charAt(0) == '(' && Math.abs(r - left) < 1e-4) {
                    continue;
                }
                if (range.charAt(range.length() - 1) == ')' && Math.abs(right - r) < 1e-4) {
                    continue;
                }
                break;
            }
        } else {
//            runTime=0;

        }
        return r;
    }

    public static Map<String, Object> randomValue(JSONObject paramJs) {
        Integer accuracy = paramJs.getInteger("accuracy");
        String range = paramJs.getString("range");

        //定义返回值
        Map<String, Object> rMap=new HashMap<>();
        if (accuracy != -1) {
            Random rand = new Random();
            int i = range.indexOf(",");
            if (i == -1) {
                throw new RuntimeException("range 格式错误");
            }
            Double left = Double.parseDouble(range.substring(1, i));
            Double right = Double.parseDouble(range.substring(i + 1, range.length() - 1));

            //设置精度值
            Double num = Math.pow(0.1, accuracy);

            //处理报错
            if (right < left) {
                throw new RuntimeException("区间左边小于右边");
            }
            if (Math.abs(right - left) < num) {
                throw new RuntimeException("区间长度小于精度值");
            }
            if (range.charAt(0) == '(' && range.charAt(range.length() - 1) == ')' && num == (right - left)) {
                throw new RuntimeException("无法在这个开区间内取到值");
            }

            //生成一个随机数 nextDouble() 生成值区间在[0,1.0)间
            Double value = rand.nextDouble() * (right - left) + left;
            value = Double.parseDouble(String.format("%." + accuracy + "f", value));

            if (range.charAt(0) == '(' && Math.abs(value - left) < num) {
                value += num;
            }
            if (range.charAt(range.length() - 1) == ')' && Math.abs(right - value) < num) {
                value -= num;
            }

            //判断是否是整数
//            if(accuracy==0){
//                rMap.put("type","Long");
//                rMap.put("value",value.longValue());
//            }else {
//                rMap.put("type","Double");
//                rMap.put("value",value);
//            }
            rMap.put("value",value);
        }else{
            //枚举
            List rangeEnum = paramJs.getObject("range", List.class);
            Object o = rangeEnum.get(new Random().nextInt(rangeEnum.size()));
            String type = paramJs.getString("type");
            if(type==null){
                throw new RuntimeException("枚举类未设置类型");
            }
            if("String".equals(type)){
                rMap.put("type","String");
                rMap.put("value",o);
            }else if("Long".equals(type)){
                rMap.put("type","Long");
                rMap.put("value",o);
            }else if("Double".equals(type)){
                rMap.put("type","Double");
                rMap.put("value",o);
            }else{
                throw new RuntimeException("未知类型");
            }
        }
        return rMap;
    }


}
